package handler

import (
	"github.com/gin-gonic/gin"
	"golang.org/x/net/context"
	"net/http"
	"product-announcement/api/request"
	"product-announcement/global"
	"product-announcement/internal/dao"
	"product-announcement/internal/model"
	"time"
)

func AddAnnouncement(c *gin.Context) {
	var req request.AddAnnouncementForm
	var announcement model.Announcement

	if req.Title == "" || req.Content == "" {
		c.JSON(http.StatusOK, gin.H{
			"code": 100000,
			"msg":  "验证失败",
			"data": nil,
		})
		return
	}

	announcement = model.Announcement{
		Title:   req.Title,
		Content: req.Content,
	}

	err := dao.AddFile(&announcement)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 100000,
			"msg":  "发布失败",
			"data": false,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "发布成功",
		"data": announcement,
	})

	global.RDB.Set(context.Background(), "received_notice", announcement, time.Hour*24)

	global.RDB.Set(context.Background(), "latest_notice", announcement, time.Hour*24)

	global.RDB.Set(context.Background(), "order_queue", announcement, time.Hour*24)

}

func ListAnnouncement(c *gin.Context) {
	
}